package list;

public class LinkedList<E> implements IList<E> {
	private int size;
	private Node<E> head;
	private Node<E> tail;
	
	public LinkedList() {
		super();
		this.size = 0;
		this.head = null;
		this.tail = null;
	}

	@Override
	public int size() {
		return size;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}

	@Override
	public boolean isFirst(E e) {
		return isEmpty() ? false : head.getElement().equals(e);
	}

	@Override
	public boolean isLast(E e) {
		return isEmpty() ? false : tail.getElement().equals(e);
	}

	@Override
	public E first() {
		return (isEmpty() ? null : head.getElement());
	}

	@Override
	public E last() {
		return (isEmpty() ? null : tail.getElement());
	}

	@Override
	public E prev(int index) throws IndexOutOfBoundsException {
		if(index <= 0 || index >= size) {
			throw new IndexOutOfBoundsException();
		}
		Node<E> temp = head;
		for(int i = 0; i < index - 1; i++) {
			temp = temp.getNext();
		}
		return temp.getElement();
	}

	@Override
	public E next(int index) throws IndexOutOfBoundsException {
		//TODO
		if(index < 0 || index >= size) {
			throw new IndexOutOfBoundsException();
		}
		Node<E> temp = head;
		for(int i = 0; i < index - 1; i++) {
			temp = temp.getNext();
		}
		return temp.getElement();
	}

	@Override
	public E replaceElement(int index, E e) throws IndexOutOfBoundsException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public E remove(int index) throws IndexOutOfBoundsException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void swapElements(int index1, int index2)
			throws IndexOutOfBoundsException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void insertBefore(int index, E e) throws IndexOutOfBoundsException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void insertAfter(int index, E e) throws IndexOutOfBoundsException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void insertFirst(E e) {
		Node<E> n = new Node<E>(e, head);
		head = n;
		size++;
		if(size == 1) {
			tail = head;
		}
	}

	@Override
	public void insertLast(E e) {
		if(size == 0) {
			insertFirst(e);
		} else {
			Node<E> n = new Node<E>(e, null);
			tail.setNext(n);
			tail = n;
			size++;
		}
	}
	
	public String toString() {
		String out = "LinkedList [";
		Node<E> temp = head;
		for(int i = 0; i < size; i++) {
			out += temp.getElement().toString();
			temp = temp.getNext();
		}
		out += "]";
		return out;
	}
}
